﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Xml;
using System.Threading;
namespace IP1
{
    class Program
    {

        static void Main(string[] args)
        {
            string pathin = "F://ip.txt";
            StreamReader sr = new StreamReader(pathin, Encoding.Default);
            String line;
            string pathout1 = "F://ip_out.txt";
            FileStream fs1 = new FileStream(pathout1, FileMode.Create);
            StreamWriter sw1 = new StreamWriter(fs1, Encoding.Default);
            string pathout2 = "F://ip_out_Nocity.txt";
            FileStream fs2 = new FileStream(pathout2, FileMode.Create);
            StreamWriter sw2 = new StreamWriter(fs2, Encoding.Default);
            WebClient wc = new WebClient();
            //line = sr.ReadLine();
            while ((line = sr.ReadLine()) != null)
            {
                string sURL = "http://www.ip138.com/ips138.asp?ip=" + line;
                string data = Encoding.Default.GetString(wc.DownloadData(string.Format(sURL)));
                string strtempa = "<li>本站主数据：";
                string strtempb = "</li><li>参考数据一";
                int IndexofA = data.IndexOf(strtempa);
                int IndexofB = data.IndexOf(strtempb);
                string Ru = data.Substring(IndexofA + 10, IndexofB - IndexofA - 10);
                System.Console.WriteLine(line + "-" + Ru);
                if ((Ru.IndexOf("市") >= 0) || (Ru.IndexOf("县") >= 0) || (Ru.IndexOf("自治州") >= 0))
                {
                    sw1.WriteLine(line + "-" + Ru);
                }
                else
                {
                    sw2.WriteLine(line + "-" + Ru);
                }

                Thread.Sleep(3000);
            }
            sw1.Flush();
            sw1.Close();
            fs1.Close();
            sw2.Flush();
            sw2.Close();
            fs2.Close();
            // while ((line = sr.ReadLine()) != null)
            //{
            //  sw.Write(line+"\r\n");
            //}
        }

    }
}

